package com.cennavi.dbs;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;

public class MongoPool {

	private static Properties props = new Properties();

	private static Mongo mongo = null;
	
	private static String db = null;
	
	private static String username = null;
	
	private static String password = null;

	public static void init(String confPath) throws FileNotFoundException,
			IOException {
		props.load(new FileInputStream(confPath));

		String ip = props.getProperty("ip");

		int port = Integer.parseInt(props.getProperty("port"));

		db = props.getProperty("db.name");

		username = props.getProperty("username");

		password = props.getProperty("password");

		int poolSize = Integer.parseInt(props.getProperty("pool.size"));

		int blockSize = Integer.parseInt(props.getProperty("block.size"));

		mongo = new Mongo(ip, port);
		MongoOptions opt = mongo.getMongoOptions();
		opt.connectionsPerHost = poolSize;
		opt.threadsAllowedToBlockForConnectionMultiplier = blockSize;
	}

	public static DB getDB() {
		DB d = mongo.getDB(db);
		
		d.addUser(username, password.toCharArray());
		
		return d;
	}

}
